/**
 * Porting from linux kernel 2.6.24
 */
#ifndef _KFIFO_H
#define _KFIFO_H

#include <pthread.h>

#define min(a,b) (((a) < (b)) ? (a) : (b))

/*
 *  Determine whether some value is a power of two, where zero is
 * *not* considered a power of two.
 */
static inline __attribute__((const))
int is_power_of_2(unsigned long n)
{
	return (n != 0 && ((n & (n - 1)) == 0));
}

struct kfifo {
	unsigned char *buffer;	/* the buffer holding the data */
	unsigned int size;	/* the size of the allocated buffer */
	unsigned int in;	/* data is added at offset (in % size) */
	unsigned int out;	/* data is extracted from off. (out % size) */
	pthread_mutex_t mutex;	/* protects concurrent modifications */
};

struct kfifo *kfifo_init(unsigned char *buffer, unsigned int size);
void kfifo_free(struct kfifo *fifo);

unsigned int kfifo_put_nolock(struct kfifo *fifo,
			 unsigned char *buffer, unsigned int len);
unsigned int kfifo_put(struct kfifo *fifo,
			 unsigned char *buffer, unsigned int len);
unsigned int kfifo_get_nolock(struct kfifo *fifo,
			 unsigned char *buffer, unsigned int len);
unsigned int kfifo_get(struct kfifo *fifo,
			 unsigned char *buffer, unsigned int len);
unsigned int kfifo_get_in_out(struct kfifo *fifo,
			unsigned int *in, unsigned int *out);
int kfifo_get_rest_data_length(struct kfifo *fifo);

#endif